/*
 * usb_sender.cpp
 *
 *  Created on: 2021年5月11日
 *      Author: xrj
 */

#include "sender.hpp"
#ifdef DATAFLOW_INC_SENDER_HPP_
#include "buffhead.hpp"


/*
 * example code, will use each below:
 *
 * extern USBD_HandleTypeDef hUsbDeviceFS;
 * extern ADC_HandleTypeDef hadc1;
 * extern TIM_HandleTypeDef htim2;
 * C_ADCEx adc = C_ADCEx(&hadc1, &htim2);
 * Buffer buff = Buffer(2);
 *
 * buff.Init();
 * buff.remalloc(128);
 * adc.set_SR_sps(10000);
 * adc.conn_buff(&buff.w_head);
 */

#ifdef USE_FREERTOS
#ifdef USE_USB
/*
 * send buffer data via usb
 * example:
 * osThreadAttr_t attr={.name="usb_sender"};
 * SenderParam param = {.buf=buff, .pdev=&hUsbDeviceFS, 32}; //2*32=64 Byte per pack max
 * osThreadNew(&UsbSender_TaskFunc, &param, &attr);
 * adc.DMA_cycle(100);
 */
void UsbSender_TaskFunc(void *argument)
{
	SenderParam *param = (SenderParam*)argument;
	BuffHeadRead head = BuffHeadRead(*param->r_heads, param->r_heads->new_head());
	u8* p;
	u32 be = head.bytes_elem();
	while(1){
		p = (u8*)head.get_frames(param->n);
		HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
		while(USBD_CUSTOM_HID_SendReport(param->pdev, p, param->n*be)!=USBD_OK);
		HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
	}
}
#endif

#endif
#endif
